using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.Common;
using JlPay.Sdk.Models.Trans.Common;

namespace JlPay.Sdk.Examples.Trans.Common
{
    /// <summary>
    /// 交易 - Common获取OpenID功能使用示例
    /// </summary>
    public class GetOpenidDemo
    {
        /// <summary>
        /// 构建获取OpenID请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static GetOpenidRequest BuildGetOpenidRequest()
        {
            var request = new GetOpenidRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.PayType = "wxpay";
            request.AuthCode = "131208648101095993";
            request.SubAppid = "wxid_129752pfasf1671";
            request.AppUpIdentifier = "com.example.app";
            request.DeviceInfo = "N50007A8890";
            request.MchCreateIp = "123.12.12.123";
            
            return request;
        }

        /// <summary>
        /// 获取OpenID示例
        /// </summary>
        public static async Task GetOpenidDemoAsync()
        {
            Console.WriteLine("开始执行获取OpenID示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var getOpenidService = new GetOpenidService(config);

            // 创建获取OpenID请求
            var request = BuildGetOpenidRequest();

            try
            {
                // 发送请求
                var response = await getOpenidService.GetOpenidAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("获取OpenID", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new GetOpenidResponse();
                response.HandleResponseWithException("获取OpenID", config, ex);
            }

            Console.WriteLine("获取OpenID示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 获取OpenID示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await GetOpenidDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 获取OpenID示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 